#include "exceptions.h"
#include "log.h"
#include "schema.h"
#include "table.h"
#include "timer.h"

#include <cstdio>
/*
    SELECT/UPDATE/DELETE query plans
        match constraints to indices
        match?
            -> yes: use highest-cardinality index
            -> no: do table scan

    Table scan
        obtain read lock
        obtain iterator on table
        for each row:
            constraint match?

    INSERT query plan
        unique constraint on any column?

*/

using namespace Database::Logging;

int main(int argc, char **argv)
{
	try
	{
        Timer tProgram = Timer("Program");

	    // Configure logging
	    // TODO - read log file from conf
	    if(!LogWriter::open("database.log"))
	    {
            ::fprintf(::stderr, "Unable to open log file 'database.log'\n");
            ::fflush(::stderr);
	    }

        Database::Schema s;

        Timer tOpen = Timer("open table database1.table1");

        Database::Table& t = s.database("database1").openTable("table1");

        t.lockWrite();
        t.lockRead();

        t.unlockRead();
        t.unlockWrite();

        t.close();

        tOpen.stop();

        Log(LL_DEBUG) << "hello world";
	}
	catch(Database::EDatabase e)
	{
		::printf("Fatal error %s\n", e.what());		// Using printf() for exception-safety
		return 1;
	}

    LogWriter::close();
	return 0;
}
